﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace NEM.Networking_Classes
{
    public static class NetworkFunctions
    {
        #region Private Variables / Constants

        #endregion

        #region Properties

        #endregion

        #region Methods

        #region Windows API Calls


        #endregion

        public static PingReply PingAddress(string pAddress,out bool pSuccess)
        {
            PingReply lvResult = null;
            Ping lvPinger = new Ping();

            pSuccess = false;

            Globals.Logger.LogMessage(string.Format("*****Enter Function***** \nFunction: [{0}] \n Parameters: [{1}]", "PingAddress(string pAddress,out bool pSuccess)", pAddress));
            try
            {
                PingReply lvPingResult = lvPinger.Send(pAddress);
                if (lvPingResult.Status == IPStatus.Success)
                {
                    pSuccess = true;
                    
                }
                else
                {
                    pSuccess = false;
                }
            }
            catch (Exception ex)
            {
                pSuccess = false;
                Globals.Logger.LogMessage(string.Format("Error trying to ping address {0}", pAddress),ex);
            }


            return lvResult;
        }

        public static string GetMacAddress(string pAddress, out bool pSuccess)
        {
            string lvMacAddress = string.Empty;
            string lvOutput = string.Empty;
            bool lvSuccess = true;

            Process lvProcess = new Process();

            try 
	        {	        
		        //Setup Process
                    lvProcess.StartInfo.FileName = "arp";
                    lvProcess.StartInfo.Arguments = "-a " + pAddress;
                    lvProcess.StartInfo.UseShellExecute = false;
                    lvProcess.StartInfo.RedirectStandardOutput = true;
                    lvProcess.StartInfo.CreateNoWindow = true;
                    lvProcess.Start();

                    //Check output
                    lvOutput = lvProcess.StandardOutput.ReadToEnd();
            
                    //Split String to get the Mac Address
                    string[] lvSplitResults = lvOutput.Split('-');
                    if (lvSplitResults.Length >= 8)
                    {
                        lvMacAddress = lvSplitResults[3].Substring(Math.Max(0, lvSplitResults[3].Length - 2)) + "-" + lvSplitResults[4] + "-" + lvSplitResults[5] + "-" + lvSplitResults[6] + "-" + lvSplitResults[7] + "-" +
                               lvSplitResults[8].Substring(0, 2);
                    }
                    else
                    {
                        lvSuccess = false;
                        lvMacAddress = string.Empty;
                    }
	        }
	        catch (Exception ex)
	        {
                lvSuccess = false;
                lvMacAddress = ex.Message;
	        }

            //Update return Values and send
            pSuccess = lvSuccess;
            return lvMacAddress;

        }

        public static bool TestProxyCredentials(string pUriString)
        {
            bool lvReturn = false;

            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(pUriString);
                request.Timeout = 5000;
                request.Credentials = CredentialCache.DefaultNetworkCredentials;
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                if (response.StatusCode == HttpStatusCode.OK)
                {
                    Console.WriteLine("IsSIPServerAvailable: " + response.StatusCode);
                    lvReturn = true;
                }
            }
            catch (Exception ex)
            {
                //System.Windows.Forms.MessageBox.Show(ex.Message);
            }


            System.Windows.Forms.MessageBox.Show(lvReturn.ToString());
            return lvReturn;
        }

        #endregion


    }
}
